package com.myjob.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.util.pattern.PathPatternParser;

import java.util.Collections;

/**
 * 解决跨域问题
 * @author Chen Yuan
 * @create 2021-03-20  17:14
 */
@Configuration
public class CorsConfig {

	public CorsWebFilter corsWebFilter(){
		CorsConfiguration configuration = new CorsConfiguration();
		configuration.setAllowedMethods(Collections.singletonList("*"));
		configuration.setAllowedHeaders(Collections.singletonList("*"));
		configuration.setAllowedOrigins(Collections.singletonList("*"));
		configuration.setAllowCredentials(true);

		// 添加路径匹配方式
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/**", configuration);
		CorsWebFilter corsFilter = new CorsWebFilter((CorsConfigurationSource) source);
		return corsFilter;
	}
}
